//
//  ThreeMealsViewController.m
//  Food
//
//  Created by 15 on 15/7/14.
//  Copyright (c) 2015年 李志远. All rights reserved.
//

#import "ThreeMealsViewController.h"
#import "ThreeMealTableViewCell.h"
#import "BaseHeader.h"
#import "AFNet.h"
#import "ThreeMealModel.h"
#import "ThreeViewDetailsViewController.h"
#import "MBProgressHUD.h"
#import "MJRefresh.h"
@interface ThreeMealsViewController ()<UITableViewDataSource, UITableViewDelegate, MBProgressHUDDelegate>

@end

@implementation ThreeMealsViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arrFooter = [NSMutableArray array];
        self.arrHeader = [NSMutableArray array];
        self.arrThree = [NSMutableArray array];
    }
    return self;
}

- (void)createProgress
{
    self.progress = [[MBProgressHUD alloc] initWithView:self.view];
    self.progress.labelText = @"稍等";
    self.progress.detailsLabelText = @"正在加载...";
    self.progress.delegate = self;
    [self.progress show:YES];
    [self.view addSubview:self.progress];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self getDataThreeMealsMenu];
    Reachability *r = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    if (r.currentReachabilityStatus == NotReachable) {
        UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络连接失败" delegate:self cancelButtonTitle:@"好" otherButtonTitles:nil];
        [alert1 show];
        [alert1 release];
        return;
    }
        [self createProgress];
}

- (void)createView
{
    self.table= [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 99) style:UITableViewStylePlain];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    [self.table release];
    [self.table registerClass:[ThreeMealTableViewCell class] forCellReuseIdentifier:@"threeMeal"];
}

- (void)getDataThreeMealsMenu
{
    self.arrThree = [NSMutableArray array];
    self.page = 1;
    NSString *strHttp = [NSString stringWithFormat:@"%@%@&page=%ld", ThreeMeal, self.strType, self.page];
    [AFNet connectNetGetDataWithURLString:strHttp parameters:nil finishblock:^(id result) {
        Reachability *r = [Reachability reachabilityWithHostName:@"www.baidu.com"];
        if (r.currentReachabilityStatus == NotReachable) {
            UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络连接失败" delegate:self cancelButtonTitle:@"好" otherButtonTitles:nil];
            [alert1 show];
            [alert1 release];
            return;
        }
        self.progress.hidden = YES;
        NSArray *arr = result[@"data"];
        for (NSDictionary *d in arr) {
            ThreeMealModel *model = [[[ThreeMealModel alloc] initWithDic:d] autorelease];
            [self.arrThree addObject:model];
        }
        [self createView];
        [self setUpReFresh];
    }];
}
#pragma mark - 刷新与拉取
- (void)setUpReFresh
{
    // 下拉刷新
    [self.table addHeaderWithTarget:self action:@selector(headerRereshing)];
    // 上拉加载
    [self.table addFooterWithTarget:self action:@selector(footerRereshing)];
    [self.table headerBeginRefreshing];
    // 设置文字
    self.table.headerPullToRefreshText = @"下拉刷新";
    self.table.headerReleaseToRefreshText = @"松开立刻刷新";
    self.table.headerRefreshingText = @"刷新中,请稍后...";

    self.table.footerPullToRefreshText = @"上拉加载数据";
    self.table.footerReleaseToRefreshText = @"松开立刻加载数据";
    self.table.footerRefreshingText = @"加载数据中,请稍后...";
}

#pragma mark - 下拉刷新方法
-(void)headerRereshing
{
    Reachability *r = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    if (r.currentReachabilityStatus == NotReachable) {
        UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络连接失败" delegate:self cancelButtonTitle:@"好" otherButtonTitles:nil];
        [alert1 show];
        [alert1 release];
        return;
    }
    [self.arrHeader removeAllObjects];
    self.page = 1;
    NSString *strHttp = [NSString stringWithFormat:@"%@%@&page=%ld", ThreeMeal, self.strType, self.page];
    [AFNet connectNetGetDataWithURLString:strHttp parameters:nil finishblock:^(id result) {
        NSArray *arr = result[@"data"];
        for (NSDictionary *d in arr) {
            ThreeMealModel *model = [[ThreeMealModel alloc] initWithDic:d];
            [self.arrHeader addObject:model];
        }
        [self.arrThree removeAllObjects];
        [self.arrThree addObjectsFromArray:self.arrHeader];
    }];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.table reloadData];
        [self.table headerEndRefreshing];
    });
    self.page = 2;
}
#pragma mark - 上拉加载
- (void)footerRereshing
{
    Reachability *r = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    if (r.currentReachabilityStatus == NotReachable) {
        UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络连接失败" delegate:self cancelButtonTitle:@"好" otherButtonTitles:nil];
        [alert1 show];
        [alert1 release];
        return;
    }
    [self.arrFooter removeAllObjects];
    NSString *strHttp = [NSString stringWithFormat:@"%@%@&page=%ld", ThreeMeal, self.strType, self.page++];
    [AFNet connectNetGetDataWithURLString:strHttp parameters:nil finishblock:^(id result) {
        NSArray *arr = result[@"data"];
        for (NSDictionary *d in arr) {
            ThreeMealModel *model = [[ThreeMealModel alloc] initWithDic:d];
            [self.arrFooter addObject:model];
        }
        [self.arrThree addObjectsFromArray:self.arrFooter];
        [self.table reloadData];
    }];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.table reloadData];
        [self.table footerEndRefreshing];
    });
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrThree.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.table deselectRowAtIndexPath:indexPath animated:YES];
    ThreeViewDetailsViewController *detail = [[ThreeViewDetailsViewController alloc] init];
    detail.Code = [self.arrThree[indexPath.row] code];
    detail.strImg = [self.arrThree[indexPath.row] img];
    detail.judge = 1;
    [self.navigationController pushViewController:detail animated:YES];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ThreeMealTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"threeMeal"];
    cell.model = self.arrThree[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
